<?php


namespace app\api\event;

use app\api\model\plus\coupon\UserCoupon;
use \app\common\model\plus\agent\Setting;
use app\api\model\user\User;
use app\common\enum\order\OrderTypeEnum;
use app\common\model\plus\coupon\UserPromoterCoupon;
use app\common\model\plus\discount\DiscountGitOrder;
use app\common\model\shopper\Shopper;
use app\common\service\message\MessageService;
use app\common\service\order\OrderPrinterService;
use app\api\model\order\Order;
use app\api\service\order\paysuccess\source\PaySourceSuccessFactory;
use app\api\model\product\Product as ProductModel;
use app\common\model\european\EuropeanUserPicking;

class PaySuccess
{
	public $order;
	public $appId;
	public $orderType;


	public function handle(Order $order)
	{
		$this->order = $order;
		$this->appId = $order['app_id'];
		$this->orderType = OrderTypeEnum::MASTER;
		// 订单公共业务
		$this->onCommonEvent();
		// 订单来源回调业务
		$this->onSourceCallback();

		$this -> pushMsg($order['store_id']);//推送消息

        //购买参与竞猜的商品可得竞猜次数
        EuropeanUserPicking::updateOrderData($order['order_id']);

        //更改赠品订单的状态
        $this -> updateGiftProductOrder();
		return true;
	}

    /**
     * 支付成功后推送
     * @param $store_id
     * @return void
     */
    public function pushMsg($store_id)
    {
        $msg = '您有新的订单，请及时查收';
        $result_data = [];
        $result_data['msg'] = $msg;
        $result_data['type'] = 'success';
        $result_data_json = json_encode($result_data);
        Shopper::messagePush($store_id,$result_data_json,1);//推送消息
    }

    /**
     *更改赠品订单的状态
     * updateGiftProductOrder
     * void
     * @return void
     * @Author kevin
     * @date 2024/4/8 15:08
     */
    public function updateGiftProductOrder()
    {
        $order_id = $this->order['order_id'];
        return (new DiscountGitOrder()) -> where('order_id',$order_id) -> update(['pay_status' => 20]);
    }

	/**
	 * 订单公共业务
	 */
	private function onCommonEvent()
	{
		// 发送用户消息通知
        //(new MessageService)->payment($this->order, $this->orderType);
		// 发送商家消息通知
        //(new MessageService)->supplier($this->order, $this->orderType);
        //发送配送员消息通知
        // 20240517 Mr.Z 处理不正确接口问题！
        (new MessageService)->storeClerk($this->order);
		// 小票打印
//        (new OrderPrinterService)->printTicket($this->order);
		// 更新供应商销量
		(new ProductModel)->reSupplierTotalSales($this->order['shop_supplier_id']);
		// 20240223 joey 更新用户累计付款订单数，推广奖励券发放
		 $this->onOrderNumCoupon();
	}

	/**
	 * 订单来源回调业务
	 */
	private function onSourceCallback()
	{
		$model = PaySourceSuccessFactory::getFactory($this->order['order_source']);
		return $model->onPaySuccess($this->order);
	}

	/**
	 * 更新用户累计付款订单数，给上级发放推广奖励券
	 * @return void
	 * @Author joey
	 * @date 2024/2/29 15:26
	 */
	private function onOrderNumCoupon()
	{
        // 20240524 Mr.Z 更新下单数量
		$model = User::detail($this->order['user_id']);
//		// if($model['order_num'] == 0) {
//		// 发放首单推广优惠券
//		$settings = Setting::getItem('basic');
//		if(isset($settings['first_money']) && $this->order['pay_price']>=$settings['first_money']){
//			if (isset($settings['first_give_coupon']) && $settings['first_give_coupon'] == 1 && $settings['become_coupon_ids']) {
//				 is_array($settings['become_coupon_ids']) || $settings['become_coupon_ids'] = [$settings['become_coupon_ids']];
//				//赠送给上级
//				$CheckGet = UserPromoterCoupon::checkgetcoupon($this->order['user_id'],$model['referee_id']);
//				if($CheckGet==false){
//					(new UserCoupon())->addUserCoupon($settings['become_coupon_ids'], $model['referee_id']);
//					$PromoterCouponModel = new UserPromoterCoupon();
//					foreach($settings['become_coupon_ids'] as $coupon_id) {
//						$PromoterCouponModel->add($model['referee_id'], $coupon_id, $this->order['user_id']);
//					}
//				}
//			}
//		}
//		// }
		return $model->setOrderNum(1);
	}
}
